from django.contrib import admin
from goods.models import *
from celery_tasks.tasks_list import generate_static_index_html
from django.core.cache import cache
# Register your models here.


class BaseAdmin(admin.ModelAdmin):
    """商品活动信息的管理类， 运营人员在后台发布内容，异步生成静态页面"""
    def save_model(self, request, obj, form, change):
        """后台保存数据时使用"""

    # 要保存对象调用save时，异步调用
        obj.save()
        generate_static_index_html.delay()
        # 当修改了后台数据的时候也要刷新页面，重新设置缓存
        cache.delete('index_page_data')

    def delete_model(self, request, obj):
        obj.delete()
        generate_static_index_html.delay()
        cache.delete('index_page_data')


class IndexPromotionBannerAdmin(BaseAdmin):
    """商品活管理类
        如果有比如分页之类的逻辑的在单独写在这里
    """
    pass

class GoodsCategoryAdmin(BaseAdmin):
    pass

class GoodsAdmin(BaseAdmin):
    pass


class GoodsSKUAdmin(BaseAdmin):
    pass


class IndexCategoryGoodsBannerAdmin(BaseAdmin):
    pass


admin.site.register(GoodsCategory, GoodsCategoryAdmin)
admin.site.register(Goods, GoodsAdmin)
admin.site.register(GoodsSKU, GoodsSKUAdmin)
admin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)
admin.site.register(IndexCategoryGoodsBanner, IndexCategoryGoodsBannerAdmin)